<template>
  <PageWrapper title="新增或编辑活动" contentBackground contentClass="p-4" @back="goBack">
    <BasicForm
      @register="registerForm"
      :labelWidth="100"
      :schemas="schemas"
      :actionColOptions="{ span: 24 }"
      :showResetButton="false"
      @submit="handleSubmit"
      class="edit"
    />
  </PageWrapper>
</template>
<script lang="ts">
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { defineComponent, ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { useGo } from '/@/hooks/web/usePage';
  import { useRouter } from 'vue-router';
  import { getDomain, saveOrUpdateDomain } from '/@/api/ccb/ccbList';

  let infoActivityNo = ref('');
  let infoDomainName = ref('');
  let type = ref<boolean>(false);
  const schemas: FormSchema[] = [
    {
      field: 'activityNo',
      component: 'Input',
      defaultValue: infoActivityNo,
      label: '活动编号',
      componentProps: {
        placeholder: '请输入活动名称',
      },
      colProps: {
        span: 16,
      },
      required: true,
    },
    {
      field: 'domainName',
      component: 'Input',
      defaultValue: infoDomainName,
      label: '二级域名',
      suffix: '-h5.benlai.com',
      colProps: {
        span: 16,
      },
      componentProps: {
        placeholder: '请输入二级域名(限数字和英文字母)',
        oninput: "value=value.replace(/[\\W]/g,'')",
      },
      required: true,
    },
  ];
  const [registerForm, {}] = useForm({
    submitButtonOptions: { text: '提交' },
  });
  export default defineComponent({
    name: 'DomainEdit',
    components: { BasicForm, PageWrapper },
    setup() {
      const go = useGo();
      const { currentRoute } = useRouter();
      const { createMessage } = useMessage();

      function goBack() {
        go('/ccb/domainList');
      }

      (async () => {
        const { activityNo } = currentRoute.value.query;
        if (activityNo) {
          const res = await getDomain({ activityNo: activityNo as string });
          console.log(res);
          infoActivityNo.value = res.activityNo;
          infoDomainName.value = res.domainName;
          type.value = true;
        } else {
          infoActivityNo.value = '';
          infoDomainName.value = '';
          type.value = true;
        }
      })();

      return {
        registerForm,
        schemas,
        infoActivityNo,
        handleSubmit: async (values: any) => {
          if (type.value) {
            const res = await saveOrUpdateDomain({
              activityNo: values.activityNo,
              domainName: values.domainName,
            });
            if (!res) {
              createMessage.error('新增失败，域名已存在或其他原因');
            } else {
              createMessage.success('编辑成功');
            }
            go('/ccb/domainList');
          } else {
            await saveOrUpdateDomain({
              activityNo: values.activityNo,
              domainName: values.domainName,
            });
            createMessage.success('提交成功');
            go('/ccb/domainList');
          }
        },
        goBack,
      };
    },
  });
</script>
